/**
 * @author Idika
 */

var RoomModel = Class.create({
	
	initialize:function(){
		this._room;
		this._playerList ;
		this._playerDeckList;
		this._commonAreaDeck;
		this._selfPlayerDeckModel;
		this._activePlayer;
		this._state;
		this._componentId;
		this._owner; 
		this._selfPlayer;
		this._versionNo = 1;
	},
	
	init:function(o){
		this._room = o['room'];
		this._componentId = o['componentId'];
		this._selfPlayer = o['selfPlayer'];
		this._owner = this._room.ownerName;
		this._playerList = [];
		this._playerDeckList = [];
		this._commonAreaDeck = [];
		this.gameDeskInitialized = new Event(this);
		this.newUserJoinedRoom = new Event(this);
		this.userFinishPlay = new Event(this);
		this.userActivated = new Event(this);
		
		
	},
	
	getRoom:function(){
		return this._room;
	},
	
	getAllUser:function(){
		return this._playerList;
	},
	
	getSelfPlayer:function (){
		return this._selfPlayer;
	},
	
	getOwner:function (){
		return this._owner;
	},
	
	
	initializeGameDesk: function(){
		this.gameDeskInitialized.notify({});
	},
	
	initSelfDeck:function(){
		
	},
	
	updatePlayersDeck:function(){
		
	},
	
	newUserJoined:function(user){
		this._playerList.push(user);
		this.newUserJoinedRoom.notify({'user':user});
	},
	
	updateCommonAreaDeck:function(){
		
	},
	
	updateSelfDeckList:function(){
		
	},
	updatePlayerList:function(){
		
	},
	
	getRole:function(){
		RoomService.getRoleCards(this._versionNo);
	},
	
	getCharacter:function(){
		RoomService.getCharacterCards(this._versionNo);
	},
	
	startGame:function(){
		RoomService.startGame(this._versionNo);
	},
	
	pass:function(){
		var isFinishedPlay = this._selfPlayerDeckModel.finishPlay();
		if(isFinishedPlay){
			RoomService.finishPlay(this._versionNo);
		}	
	},
	
	addPlayerDeckModel:function(playerDeckModel){
		this._playerDeckList.push(playerDeckModel);
	},
	
	setSelfPlayerDeckModel:function(selfPlayerDeckModel){
		this._selfPlayerDeckModel = selfPlayerDeckModel;
	},
	
	findPlayerDeck:function(userName){
		for(var i=0;i<= this._playerDeckList.length; i++){
			if(userName == this._playerDeckList[i].name){
				return this._playerDeckList[i];
			}
		}
		return null;
	},
	
	
	updateRole:function(roleCardData){
		//var selfRoleCard = data[this._selfPlayerDeckModel.getPlayerName()];
		if(roleCardData != null) this._selfPlayerDeckModel.updateRole(roleCardData);
		//var kingUser = this.findPlayerDeck(data['king'].name);
		//kingUser.updateRole(data['king'].roleCard);
	},
	
	updateKingRole:function(user){
		var kingUser = this.findPlayerDeck(user.name);
		kingUser.updateRole(user.roleCard);
	},
	
	updateCharacterCard:function(data){
		this._selfPlayerDeckModel.updateCharacter(data[0][this._selfPlayerDeckModel.getPlayerName()]);
		for(var i=0;i<= this._playerDeckList.length; i++){
//				this._playerDeckList[i].updateCharacter(data[0][this._playerDeckList[i].getPlayerName()]);
		}
		
	},
	
	readyToStart:function(data){
		this._selfPlayerDeckModel.readyToStart(true);
//		this._playerDeckList.updateLifeCount(data[0][this._selfPlayerDeckModel.getPlayerName()].blood);
//		for(var i=0;i<= this._playerDeckList.length; i++){
//				this._playerDeckList[i].updateCharacter(data[0][this._playerDeckList[i].getPlayerName()]);
//		}
	},
	
	updateCardList:function(cardList){
		this._selfPlayerDeckModel.updateCardList(cardList);
		
	},
	
	updateDataVersion : function(version){
		this._versionNo = version;
		//$('server_data_version').innerHTML = this.version;
	},
	getDataVersion : function(version){
		return this._versionNo;
	},
	errorMsgShow : function(error){
		alert(error);
	},
	
	setActive:function(isActive){
		this._selfPlayerDeckModel.setActive(isActive);
		this.userActivated.notify({'isActive':isActive});
	},
	setActiveCards:function(cards){
		this._selfPlayerDeckModel.setActiveCards(cards);
		this.userActivated.notify({'isActive':true});
	},
	
	registerAttackableUser:function(userList){
		this._selfPlayerDeckModel.registerAttackableUser(userList);
	},
	
	
	test:function(){
		
	}
//	
//	addPlayerDeckModel:function(playerDeckModel){
//		//this._playerDeckList.push(playeyDeckModel);
//	},
//	
//	removePlayerDeckModel(playeyDeckModel){
//		
//	}
//	
//	setSelfPlayerDeckModel:function(selfPlayerDeckModel){
//		this._selfPlayerDeckModel = selfPlayerDeckModel;
//	}
	

	
	
	
	
	
	
});